package com.road.dao.imp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.road.dao.INewsDao;
import com.road.dao.ISeeDao;
import com.road.model.News;
import com.road.model.See;
import com.road.model.User;
import com.road.utils.DataSourceUtils;
import com.road.utils.DruidPlugin;

public class SeeDaoImp implements ISeeDao{
//	两种连接池切换
//	QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());

	@Override
	public int saveSeeTimes(String userPhone,String newsId) throws SQLException  {
		// TODO Auto-generated method stub
		 String sql="insert into d_news_see  (phone,news_id,see_time) values(?,?,now())";
		 Object[] params= {userPhone,newsId};
		return  qr.update(sql, params);
	}

	@Override
	public List<See> getSeePopleListByNewsId(String news_id) throws SQLException {
		// TODO Auto-generated method stub
		String sql="SELECT a.*,b.head_icon,b.user_name from d_news_see as a,auther_user as b where a.phone=b.phone and a.news_id='"+news_id+"'";
	System.out.println(sql);
		return  qr.query(sql, new BeanListHandler<See>(See.class));
	}

	@Override
	public boolean isSee(String userPhone, String newsId) throws SQLException {
		// TODO Auto-generated method stub
		String sql="SELECT phone from  d_news_see where phone= '"+userPhone+"' and news_id="+newsId;
		Object result = qr.query(sql, new ScalarHandler());
		return result==null?false:true;
	}
	
	
}
